package com.pkk.penguin.frame.know.java.driven.condition.condition.simple;

import lombok.extern.slf4j.Slf4j;
import org.jsoup.helper.StringUtil;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotatedTypeMetadata;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.MethodMetadata;

/**
 * @description: windows系统的条件
 * @author: peikunkun
 * @create: 2020-07-17 17:43
 **/
@Slf4j
public class WindowCondition implements Condition {

  /**
   * Determine if the condition matches.
   *
   * @param context the condition context
   * @param metadata metadata of the {@link AnnotationMetadata class} or {@link MethodMetadata method} being checked
   * @return {@code true} if the condition matches and the component can be registered, or {@code false} to veto the
   * annotated component's registration
   */
  @Override
  public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
    final BeanDefinitionRegistry registry = context.getRegistry();
    log.info("IOC容器中是否包含bean:[window]?" + (registry.containsBeanDefinition("window")));
    //registry.registerBeanDefinition();//注册Bean
    //registry.removeBeanDefinition();//移除bean

    final Environment environment = context.getEnvironment();
    final String property = environment.getProperty("os.name");
    if (!StringUtil.isBlank(property) && property.toLowerCase().contains("window")) {
      return true;
    }
    return false;
  }
}
